package com.example.baidumap_a.entity;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.example.baidumap_a.R;
public class Ohuang {

    //可复用提示对话框
    public static void dialogshow(Context context, String a, String s) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_sign, null);
        final AlertDialog dialog = new AlertDialog.Builder(context, R.style.MyDialog).setView(view).create();
        TextView textView = view.findViewById(R.id.dialog_text_sign1);
        TextView textView1 = view.findViewById(R.id.dialog_text_sign0);
        textView1.setText(a);
        textView.setText(s);
        Button button = view.findViewById(R.id.dialog_btn_sign);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
        //需要先显示再设置大小
        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        DisplayMetrics dm = context.getResources().getDisplayMetrics();//获取屏幕分辨率
        int screenWidth = dm.widthPixels;
        int screenHeight = dm.heightPixels;
        params.width = (int) (0.7 * screenWidth);
        window.setAttributes(params);
    }
    //可复用提示对话框
    public static void dialogshow(Context context, String a, String s,View.OnClickListener onClickListener) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_sign, null);
        final AlertDialog dialog = new AlertDialog.Builder(context, R.style.MyDialog).setView(view).create();
        TextView textView = view.findViewById(R.id.dialog_text_sign1);
        TextView textView1 = view.findViewById(R.id.dialog_text_sign0);
        textView1.setText(a);
        textView.setText(s);
        Button button = view.findViewById(R.id.dialog_btn_sign);
        button.setOnClickListener(onClickListener);
        dialog.show();
        //需要先显示再设置大小
        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        DisplayMetrics dm = context.getResources().getDisplayMetrics();//获取屏幕分辨率
        int screenWidth = dm.widthPixels;
        int screenHeight = dm.heightPixels;
        params.width = (int) (0.7 * screenWidth);
        window.setAttributes(params);
    }

    //加载对话框
    public static Dialog dialogLoadingshow(Context context, String aa) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_loading, null);
        Dialog dialogLoading = new AlertDialog.Builder(context, R.style.LoadingDialog).setView(view).create();
        ImageView imageView = view.findViewById(R.id.dialog_img_loading);
        TextView textView = view.findViewById(R.id.dialog_text_loading);
        textView.setText(aa);
        Glide.with(context).load(R.drawable.load_0_gif).into(imageView);//使用Glide加载动图
        dialogLoading.show();
        Window window = dialogLoading.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        DisplayMetrics dm = context.getResources().getDisplayMetrics();//获取屏幕分辨率
        int screenWidth = dm.widthPixels;
        int screenHeight = dm.heightPixels;
        params.width = (int) (0.9 * screenWidth);
        params.y = (int) (0.05 * screenHeight);
        window.setAttributes(params);
        window.setGravity(Gravity.TOP);
        return dialogLoading;
    }
    //写入文件
    public static void writefile(Context context,String filename,String text) throws IOException {
        System.out.println("filename:"+filename+" text:"+text);
        OutputStream out=context.openFileOutput(filename, Context.MODE_PRIVATE);//覆盖
        byte[] bytes = text.getBytes();
        out.write(bytes, 0, bytes.length);
        out.close();
    }

    //读取文件
    public static String readfile(Context context,String filename) throws IOException {
        InputStream in = context.openFileInput(filename);
        byte[] bytes = new byte[1024];
        StringBuffer sb = new StringBuffer();
        int len = -1;
        while ((len = in.read(bytes)) != -1) {
            sb.append(new String(bytes, 0, len));
        }
        in.close();
        return sb.toString();
    }


//Gps权限动态申请和打开
    public static void startGps(Activity activity,Context context){
        requestPowerlocation(context,activity);
        LocationManager lm = (LocationManager)context. getSystemService(Context.LOCATION_SERVICE);
        if (!isGpsAble(lm)) {
            Toast.makeText(context, "请打开GPS~", Toast.LENGTH_SHORT).show();
            openGPS2(activity);
        }
    }

//判断Gps是否可用
    private static boolean isGpsAble(LocationManager lm) {
        return lm.isProviderEnabled(LocationManager.GPS_PROVIDER) ? true : false;
    }

    //打开设置页面让用户自己设置
    private static void openGPS2(Activity activity) {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        activity.startActivityForResult(intent,0);
    }

    private static void requestPowerlocation(Context context,Activity activity) {
//判断是否已经赋予权限
        if (ContextCompat.checkSelfPermission(context,
                Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            //如果应用之前请求过此权限但用户拒绝了请求，此方法将返回 true。
            if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
                    Manifest.permission.ACCESS_FINE_LOCATION)) {//这里可以写个对话框之类的项向用户解释为什么要申请权限，并在对话框的确认键后续再次申请权限.它在用户选择"不再询问"的情况下返回false
            } else {
                //申请权限，字符串数组内是一个或多个要申请的权限，1是申请权限结果的返回参数，在onRequestPermissionsResult可以得知申请结果
                ActivityCompat.requestPermissions(activity,
                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, 1);
            }
        }
    }
}
